package cn.huihuo.weibo.service;

import cn.huihuo.weibo.module.ParticipationModel;

/**
 * Created by zhengjunbo on 2015/11/7.
 */
public class ParticipationService {
    public static final ParticipationService service = new ParticipationService();

    /**
     * 尝试添加关注，如果有关注则不用加，没有则加
     * @param userId
     * @param productId
     */
    public void tryPutParticipation(String userId,Integer productId){
        ParticipationModel haveParticipationModel = ParticipationModel.dao.findHaveParticipationModel(userId, productId);
        if(haveParticipationModel!=null){
            return;
        }

        ParticipationModel participationModel=  new ParticipationModel();
        participationModel.set("user_id",userId).set("prod_id",productId).save();
    }


    public void updateParticipation(Integer productId){
        ParticipationModel.dao.updateByPrductId(productId);
    }

    public void updateZeroParticipation(Integer partId){
        ParticipationModel.dao.updateZero(partId);
    }

    public String findTotalParticipation(String userId){
        ParticipationModel participationModel = ParticipationModel.dao.findByUserId(userId);
        if(participationModel==null ){
            return "";
        }
        if(participationModel.getBigDecimal("size") == null){
            return "";
        }
        Long size = participationModel.getBigDecimal("size").longValue();

        if(size<=0){
            return "";
        }

        if(size>9){
            return "9+";
        }

        return ""+size;
    }
}
